package com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class ReservationOrderSendMessage extends ExecuteListener{

	public String getDescription() {
		return "预约复检审批结束，给预约人发企业微信消息。";
	}

	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "接单");
		if(flag_ok) {
			//获取流程实例ID
			String processInstId = param.getProcessInstance().getId();
			//根据流程实例ID获取任务表内数据
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_RESERVATIONORDER  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			List<Map<String , Object>> bodylist = DBSql.query("SELECT * FROM BO_EU_RESERVATIONORDER_B  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  jcbd = headlist.get(0);
			String PEOPLENAME = CoreUtil.objToStr(jcbd.get("PEOPLENAME"));//职工姓名
			String UNIT = CoreUtil.objToStr(jcbd.get("UNIT"));//职工单位
			String PHONENUM = CoreUtil.objToStr(jcbd.get("PHONENUM"));//联系电话
			String CHECKDATE = CoreUtil.objToStr(jcbd.get("CHECKDATE"));//安排复检日期
			String TIMESLOT = CoreUtil.objToStr(jcbd.get("TIMESLOT"));//时间段
			String WORKERID = CoreUtil.objToStr(jcbd.get("WORKERID"));//职工账号
			StringBuffer fhstring = new StringBuffer();//检查项目
			StringBuffer sfykfxm = new StringBuffer();//判断是否有空腹项目
			if (bodylist != null && bodylist.size() > 0) {
				for (int i = 0; i < bodylist.size(); i++) {
					Map<String , Object> dataprobleminvent = bodylist.get(i);
					String ITEMNAME = CoreUtil.objToStr(dataprobleminvent.get("ITEMNAME"));// 项目名称
					String PROJECTNO = CoreUtil.objToStr(dataprobleminvent.get("PROJECTNO"));// 项目编号
					String sqlkf ="select FASTING from BO_EU_CHECKITEM  where  projectno ='"+PROJECTNO+"'";
			        String fasting =CoreUtil.objToStr(DBSql.getString(sqlkf,"FASTING"));
			        if (i < bodylist.size() - 1) {
			        	fhstring.append(ITEMNAME);
			        	fhstring.append("、");
					} else {
						fhstring.append(ITEMNAME);
						fhstring.append("");
					}
			        sfykfxm.append(fasting);
				}
			}
			if(!"".equals(UNIT) && !"".equals(WORKERID) ) {
				String sqluserId ="select EXT5  from ORGUSER  where USERID='"+WORKERID+"'";
		        String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
		        String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+UNIT+"'";
		        List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
		        if (jbData.size() > 0 && !"".equals(weixinid)) {
		        	String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
					String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
					String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
					int index = sfykfxm.toString().indexOf("是");
					String text="";
					if (index != -1) {
						text="您好！您已预约"+CHECKDATE+" "+TIMESLOT+"复查："+fhstring.toString()+"，其中有空腹检查项目，请空腹前往卫生服务点检查。"
						+ "地址：中春路1777号闵行航天城卫生所（动力所一楼东侧），如有问题请联系021-24180834。";
					} else {
						text="您好！您已预约"+CHECKDATE+TIMESLOT+"复查："+fhstring.toString()+"，请前往卫生服务点检查。"
						+ "地址：中春路1777号闵行航天城卫生所（动力所一楼东侧），如有问题请联系021-24180834。";
					}
					String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
					weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",text,0,agentid,acessToken);
		        }
			}
			
		}
		
	}

}
